<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="项目名称">
          <el-input v-model="searchForm.proName" clearable> </el-input>
        </el-form-item>
        <el-form-item label="项目分类">
          <el-select v-model="searchForm.proFirstClassUid" placeholder="请选择" @change="changeFirstClass">
            <el-option v-for="item in firstClassNameList" :key="item.uid" :label="item.proFirstClassName"
              :value="item.uid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter" class="search-button">查询</el-button>
          <el-button type="primary" @click="showAdd" class="add-button">添加项目</el-button>
          <el-button type="primary" @click="batchEdit('上架')" class="search-button">批量上架</el-button>
          <el-button type="primary" @click="batchEdit('下架')" class="search-button">批量下架</el-button>
          <el-button @click="handleReset" class="search-button">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table ref="table" v-loading="listLoading" :data="list" element-loading-text="Loading" fit
        highlight-current-row>
        <el-table-column align="center" type="selection" width="55">
        </el-table-column>
        <el-table-column align="center" type="index" label="序号" width="55">
        </el-table-column>
        <el-table-column label="项目分类" align="center" prop="proFirstClass">
        </el-table-column>
        <el-table-column label="图片" align="center" prop="fileName">
          <template slot-scope="{ row }">
            <el-image style="width: 100px; height: 100px" :src="row.picImg" :preview-src-list="[row.picImg]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" align="center" prop="proName">
        </el-table-column>
        <el-table-column label="评估表名称" align="center" prop="evaluationName">
        </el-table-column>
        <el-table-column label="参考价格（元）" align="center" prop="referencePrice">
        </el-table-column>
        <el-table-column label="定价" align="center" prop="actualPrice">
          <template slot-scope="{ row }">
            <el-button v-if="!row.actualPrice" @click="showUpdate(row)" type="text">添加定价</el-button>
            <el-button v-else @click="showUpdate(row)" type="text">{{
              row.actualPrice
            }}</el-button>
          </template>
        </el-table-column>
       <el-table-column label="上架状态" align="center" prop="status">
                <template slot-scope="scope">
                  <span>{{ scope.row.status === '1' ? '上架' : '下架' }}</span>
                </template>
              </el-table-column>
        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="{ row }">
            <el-button @click="sj(row)" type="text">上架</el-button>
            <el-button @click="xj(row)" type="text">下架</el-button>
            <el-button @click="showDetails(row)" type="text">查看</el-button>
            <el-button @click="delHandle(row)" type="text">删除</el-button>
            <el-button @click="goConsumable(row)" type="text">耗材</el-button>
            <el-button @click="showEvaluate(row)" type="text">入户评估表</el-button>
            <el-button @click="showEdit(row)" type="text">总结</el-button>
            <!-- <el-button @click="" type="text">总结</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageIndex" :limit.sync="searchForm.pageSize"
        @pagination="getList" />
    </el-card>
    <el-dialog :title="dialogTit" :visible.sync="dialogVisible" width="80%">
      <selectMedicalProject v-if="dialogVisible" @getName="getName" :param="param"></selectMedicalProject>
    </el-dialog>
    <el-dialog title="详情" :visible.sync="detailsDialog" width="70%">
      <el-form label-position="right" label-width="120px" ref="form" :model="form">
        <el-form-item label="项目分类">
          <div>{{ form.proFirstClass }}</div>
        </el-form-item>
        <el-form-item label="项目名称">
          <div>{{ form.proName }}</div>
        </el-form-item>
        <el-form-item label="图片">
          <el-image style="width: 100px; height: 100px" :src="form.picImg" :preview-src-list="[form.picImg]">
          </el-image>
        </el-form-item>
        <el-form-item label="简述">
          <div>{{ form.sketch }}</div>
        </el-form-item>
        <el-form-item label="服务内容">
          <div v-html="form.serviceContent"></div>
        </el-form-item>
        <el-form-item label="适用人群">
          <div>{{ form.userGroup }}</div>
        </el-form-item>
        <el-form-item label="服务说明">
          <div v-html="form.proDes"></div>
        </el-form-item>
        <el-form-item label="风险告知书">
          <div v-html="form.riskNotice"></div>
        </el-form-item>
        <el-form-item label="参考价格">
          <div>{{ form.referencePrice }}</div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="detailsDialog = false" class="btn-cancel">关闭</el-button>
      </span>
    </el-dialog>
    <el-dialog title="编辑定价" :visible.sync="priceDialog" width="24%">
      <el-form label-position="right" label-width="120px" ref="form" :rules="rules" :model="form">
        <el-form-item label="参考价格" prop="referencePrice">
          <el-input-number :precision="2" v-model.trim="form.referencePrice"></el-input-number>
        </el-form-item>
        <el-form-item label="医疗服务费" prop="medicalServiceFee">
          <el-input-number :precision="2" v-model.trim="form.medicalServiceFee"></el-input-number>
        </el-form-item>
              <el-form-item label="保险费" prop="insuranceServiceFee">
                  <el-input-number :precision="2" v-model.trim="form.insuranceServiceFee"></el-input-number>
                </el-form-item>
        <el-form-item label="材料费" prop="materialFee">
          <el-input-number :precision="2" v-model.trim="form.materialFee"></el-input-number>
        </el-form-item>
        <el-form-item label="上门服务费" prop="onsiteServiceFee">
          <el-input-number :precision="2" v-model.trim="form.onsiteServiceFee"></el-input-number>
        </el-form-item>
        <el-form-item :precision="2" label="定价" prop="actualPrice">
          <el-input-number v-model.trim="form.actualPrice"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="priceDialog = false" class="btn-cancel">取 消</el-button>
        <el-button type="primary" @click="editPrice">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="选择评估表" :visible.sync="evaluateDialog" width="80%">
      <evaluate v-if="evaluateDialog" @close="selectEvaluate"></evaluate>
    </el-dialog>
    <!-- 编辑 -->
    <el-dialog :title="editTitle" :visible.sync="editDialogVisible" width="70%">
      <el-form label-position="right" label-width="120px" ref="editForm" :model="editForm">
        <el-form-item label="项目分类">
          <el-select v-model="editForm.proFirstClassUid" placeholder="请选择" @change="changeFirstClass">
            <el-option v-for="item in firstClassNameList" :key="item.uid" :label="item.proFirstClassName"
              :value="item.uid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model.trim="editForm.proName"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <uploadImg v-if="editDialogVisible" v-model.trim="editForm.picImg"></uploadImg>
        </el-form-item>
        <el-form-item label="简述">
          <el-input v-model.trim="editForm.sketch"></el-input>
        </el-form-item>
        <el-form-item label="服务内容">
          <vueUeditor :key="'serviceContent'" v-model="editForm.serviceContent">
          </vueUeditor>
        </el-form-item>
        <el-form-item label="适用人群">
          <el-input v-model.trim="editForm.userGroup"></el-input>
        </el-form-item>
        <el-form-item label="服务说明">
          <vueUeditor :key="'proDes'" v-model="editForm.proDes"></vueUeditor>
        </el-form-item>
        <el-form-item label="风险告知书">
          <vueUeditor :key="'riskNotice'" v-model="editForm.riskNotice"></vueUeditor>
        </el-form-item>
        <el-form-item label="参考价格">
          <el-input v-model.trim="editForm.referencePrice"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false" class="btn-cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import * as MedicalProject from '@/api/MedicalProject'
  import * as itemOneClass from '@/api/itemOneClass'
  import Pagination from '@/components/Pagination'
  import uploadImg from '../../components/uploadImg.vue'
  import selectMedicalProject from './selectMedicalProject.vue';
  import evaluate from './selectEvaluate.vue';
  import vueUeditor from '../../components/vueUeditor.vue'
  export default {
    components: {
      Pagination,
      uploadImg,
      selectMedicalProject,
      evaluate,
      vueUeditor
    },
    data() {
      return {
        list: null,
        listLoading: true,
        searchForm: {
          pageIndex: 1,
          pageSize: 10
        },
        total: 0,
        dialogVisible: false,
        detailsDialog: false,
        editDialogVisible: false,
        tinymceStatus: {
          serviceContent: false,
          proDes: false,
          riskNotice: false
        },
        dialogTit: '',
        editTitle: '',
        form: {
          actualPrice: '',
          insuranceServiceFee: 10
        },
        editForm: {},
        firstClassNameList: [],
        selectUid: '',
        param: {},
        priceDialog: false,
        rules: {
          actualPrice: [{
            required: true,
            message: '请输入定价',
            trigger: 'blur'
          }, ],
          medicalServiceFee: [{
            required: true,
            message: '请输入医疗服务费',
            trigger: 'blur'
          }, ],
          materialFee: [{
            required: true,
            message: '请输入材料费',
            trigger: 'blur'
          }, ],
          referencePrice: [{
            required: true,
            message: '请输入参考价格',
            trigger: 'blur'
          }, ],
          onsiteServiceFee: [{
            required: true,
            message: '请输入上门服务费',
            trigger: 'blur'
          }, ],
        },
        evaluateDialog: false,
      }
    },
    created() {
      this.getList()
      this.getClassName()
    },
    watch: {
      'form.onsiteServiceFee': function(newVal) {
        this.actualPriceSum();
      },
      'form.medicalServiceFee': function(newVal) {
        this.actualPriceSum();
      },
      'form.materialFee': function(newVal) {
        this.actualPriceSum();
      },
    },
    methods: {
      actualPriceSum() {
        let sum = Number(this.form.onsiteServiceFee) + Number(this.form.medicalServiceFee) + Number(10)
        this.form.actualPrice = sum.toFixed(2)
      },
      getList() {
        this.listLoading = true
        let param = Object.assign({}, this.searchForm, {
          hospitalUid: localStorage.getItem('hospitalUid')
        })
        MedicalProject.queryList(param).then(response => {
          this.list = response.data.rows
          this.total = response.data.total
          this.listLoading = false
        }).catch(error => {
          this.listLoading = false
          console.log(error)
        })
      },
      handleFilter() {
        this.searchForm.pageIndex = 1
        this.getList()
      },
      handleReset() {
        this.resetTemp();
        this.getList();
      },
      resetTemp() {
        this.searchForm = {
          pageIndex: 1,
          pageSize: 10
        }
      },
      showAdd() {
        this.dialogTit = '选择项目'
        this.form = {}
        this.dialogVisible = true
      },
      showEdit(row) {
        this.editTitle = '编辑'
        this.editForm = Object.assign({}, row)
        delete this.editForm.createTime
        delete this.editForm.updateTime
        this.editDialogVisible = true
      },
      showDetails(row) {
        this.form = Object.assign({}, row)
        this.detailsDialog = true
      },
      submit() {
        if (this.editTitle === '新增') {
          this.creatHandle()
        } else if (this.editTitle === '编辑') {
          this.editHandle()
        }
      },
      creatHandle() {
        MedicalProject.insert(this.form).then(response => {
          if (response.code == 200) {
            this.dialogVisible = false
            this.getList()
            this.$message.success('新增成功')
          }
        }).catch(error => {
          console.log(error)
        })
      },
      editHandle() {
        MedicalProject.edit(this.editForm).then(response => {
          if (response.code == 200) {
            this.editDialogVisible = false
            this.$message.success('修改成功')
            this.getList()

          }
        }).catch(error => {
          console.log(error)
        })
      },
      delHandle(data) {
        this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let param = {
            uid: data.uid,
          }
          MedicalProject.del(param).then(result => {
            this.$message.success(result.msg)
            this.getList()
          }).catch(error => {
            this.loading = false
            console.log(error)
          })
        }).catch(() => {
          console.log('取消删除')
        });
      },
      getClassName() {
        itemOneClass.queryByPage({}).then(response => {
          this.firstClassNameList = response.data.rows
        }).catch(error => {
          console.log(error)
        })
      },
      changeFirstClass(e) {
        let proFirstClass = ''
        this.firstClassNameList.some((item) => {
          if (item.uid === e) {
            proFirstClass = item.proFirstClassName
            return true
          }
        });
        if (this.dialogVisible) {
          this.form.proFirstClass = proFirstClass
        }
      },
      goConsumable(data) {
        this.$router.push({
          path: "/Hospital/consumableHospital",
          query: {
            uid: data.uid
          }
        })
      },
       //上架
            sj(data) {
              const param = {
                uid: data.uid,
                status: 1
              }
              MedicalProject.edit(param).then(response => {
                if (response.code == 200) {
                  this.dialogVisible = false
                  this.getList()
                  this.$message.success('上架成功')
                }
              }).catch(error => {
                console.log(error)
              })
            },
            //下架
            xj(data) {
              const param = {
                uid: data.uid,
                status: 0
              }
              MedicalProject.edit(param).then(response => {
                if (response.code == 200) {
                  this.dialogVisible = false
                  this.getList()
                  this.$message.success('下架成功')
                }
              }).catch(error => {
                console.log(error)
              })
            },
      getName(data) {
        data.map(item => {
          delete item.createTime
          delete item.updateTime
          item.hospitalUid = localStorage.getItem('hospitalUid')
          item.hospitalName = localStorage.getItem('hospitalAme')
          item.projectsUid = item.uid
        })
        MedicalProject.insertBatch(data).then(response => {
          if (response.code == 200) {
            this.dialogVisible = false
            this.getList()
            this.$message.success('新增成功')
          }
        }).catch(error => {
          console.log(error)
        })
      },
      showUpdate(data) {
        console.log('data', data);

        this.form = {
          referencePrice: null,
          medicalServiceFee: null,
          materialFee: null,
          onsiteServiceFee: null,
          actualPrice: null,
        }
        this.form = {
          referencePrice: data.referencePrice,
          medicalServiceFee: data.medicalServiceFee,
          materialFee: data.materialFee,
          onsiteServiceFee: data.onsiteServiceFee,
          actualPrice: data.actualPrice,
          insuranceServiceFee: data.insuranceServiceFee,
          uid: data.uid,
        }
        this.priceDialog = true
      },
      editPrice() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let param = {
              uid: this.form.uid,
              ...this.form
            }
            MedicalProject.edit(param).then(response => {
              if (response.code == 200) {
                this.priceDialog = false
                this.getList()
                this.$message.success('修改单价成功')
              }
            }).catch(error => {
              this.$message.error(error)
              console.log(error)
            })
          } else {
            return false;
          }
        });
      },
      editStatus(row, status) {
        let param = {
          uid: row.uid,
          status
        }
        MedicalProject.edit(param).then(response => {
          if (response.code == 200) {
            this.priceDialog = false
            this.getList()
            this.$message.success('修改单价成功')
          }
        }).catch(error => {
          console.log(error)
        })
      },
      batchEdit(status) {
        let arr = this.$refs.table.selection
        if (arr && arr.length > 0) {
          let param = []
          arr.forEach(item => {
            param.push({
              uid: item.uid,
              status: status
            })
          })
          MedicalProject.updateBatch(param).then(response => {
            if (response.code == 200) {
              this.getList()
              this.$message.success('修改成功')
            }
          }).catch(error => {
            this.$message.error(error)
            console.log(error)
          })
        } else {
          this.$message.error('请勾选数据')
        }
      },
      showEvaluate(data) {
        this.selectUid = data.uid
        this.evaluateDialog = true
      },
      selectEvaluate(data) {
        let param = {
          uid: this.selectUid,
          evaluationUid: data.uid,
          evaluationName: data.evaluationName
        }
        MedicalProject.edit(param).then(response => {
          if (response.code == 200) {
            this.getList()
            this.evaluateDialog = false
            this.$message.success('关联成功')
          }
        }).catch(error => {
          console.log(error)
        })
      },
    }
  }
</script>
